<Tree @ref="_tree"
      TItem="string"
      Checkable
      Multiple
      CheckOnClickNode="false"
      @bind-ExpandedKeys="expandedKeys"
      @bind-CheckedKeys="checkedKeys"
      @bind-SelectedKeys="selectedKeys"
      OnExpand="OnExpand"
      OnSelect="OnSelect"
      OnCheck="OnCheck"
      AutoExpandParent="autoExpandParent">
    <TreeNode Title="0-0" Key="0-0">
        <TreeNode Title="0-0-0" Key="0-0-0">
            <TreeNode Title="0-0-0-0" Key="0-0-0-0"></TreeNode>
            <TreeNode Title="0-0-0-1" Key="0-0-0-1"></TreeNode>
            <TreeNode Title="0-0-0-2" Key="0-0-0-2"></TreeNode>
        </TreeNode>
        <TreeNode Title="0-0-1" Key="0-0-1">
            <TreeNode Title="0-0-1-0" Key="0-0-1-0"></TreeNode>
            <TreeNode Title="0-0-1-1" Key="0-0-1-1"></TreeNode>
            <TreeNode Title="0-0-1-2" Key="0-0-1-2"></TreeNode>
        </TreeNode>
        <TreeNode Title="0-0-2" Key="0-0-2"></TreeNode>
    </TreeNode>
    <TreeNode Title="0-1" Key="0-1">
        <TreeNode Title="0-1-0-0" Key="0-1-0-0"></TreeNode>
        <TreeNode Title="0-1-0-1" Key="0-1-0-1" DisableCheckbox></TreeNode>
        <TreeNode Title="0-1-0-2" Key="0-1-0-2" Disabled></TreeNode>
    </TreeNode>
    <TreeNode Title="0-2" Key="0-2"></TreeNode>
</Tree>

<br />

<Tree TItem="string"
      Checkable
      Multiple
      CheckOnClickNode="false"
      @bind-ExpandedKeys="expandedKeys"
      @bind-CheckedKeys="checkedKeys"
      @bind-SelectedKeys="selectedKeys"
      OnExpand="OnExpand"
      OnSelect="OnSelect"
      OnCheck="OnCheck"
      AutoExpandParent="autoExpandParent">
    <TreeNode Title="0-0" Key="0-0">
        <TreeNode Title="0-0-0" Key="0-0-0">
            <TreeNode Title="0-0-0-0" Key="0-0-0-0"></TreeNode>
            <TreeNode Title="0-0-0-1" Key="0-0-0-1"></TreeNode>
            <TreeNode Title="0-0-0-2" Key="0-0-0-2"></TreeNode>
        </TreeNode>
        <TreeNode Title="0-0-1" Key="0-0-1">
            <TreeNode Title="0-0-1-0" Key="0-0-1-0"></TreeNode>
            <TreeNode Title="0-0-1-1" Key="0-0-1-1"></TreeNode>
            <TreeNode Title="0-0-1-2" Key="0-0-1-2"></TreeNode>
        </TreeNode>
        <TreeNode Title="0-0-2" Key="0-0-2"></TreeNode>
    </TreeNode>
    <TreeNode Title="0-1" Key="0-1">
        <TreeNode Title="0-1-0-0" Key="0-1-0-0"></TreeNode>
        <TreeNode Title="0-1-0-1" Key="0-1-0-1" DisableCheckbox></TreeNode>
        <TreeNode Title="0-1-0-2" Key="0-1-0-2" Disabled></TreeNode>
    </TreeNode>
    <TreeNode Title="0-2" Key="0-2"></TreeNode>
</Tree>

<br />

<Button OnClick="PrintCheckedNodesKey">Print Checked Nodes Key</Button>

<Button OnClick="CheckAll">Check all nodes</Button>
<Button OnClick="UncheckAll">Uncheck all nodes</Button>
<br />
<br />

<Button OnClick="PrintSelectedKeys">Print Selected Nodes Key</Button>
<Button OnClick="SelectAll">Select All</Button>
<Button OnClick="DeselectAll">Deselect All</Button>
<br />
<br />

<Button OnClick="ExpandAll">Expand All Nodes</Button>

<Button OnClick="CollapseAll">Collapse All Nodes</Button>

<br />
<br />

<Button OnClick="ExpandAllChild">Expand Node "0-0" And Expand All Child Nodes</Button>

<Button OnClick="CollapseAllChild">Collapse Node "0-0" And Collapse All Child Nodes</Button>


@if (string.IsNullOrWhiteSpace(printType) == false)
{
    <div>@printType</div>
    <ul>
        @foreach (var item in printTexts)
        {
            <li>@item</li>
        }
    </ul>
}
@code {

    string printType;

    string[] printTexts;

    Tree<string> _tree;

    string[] expandedKeys;
    string[] checkedKeys;
    string[] selectedKeys;

    bool autoExpandParent;


    private void PrintCheckedNodesKey()
    {
        printType = "CheckedNodesKey";
        printTexts = _tree.CheckedKeys;
    }

    private void CheckAll()
    {
        _tree.CheckAll();
    }

    private void UncheckAll()
    {
        _tree.UncheckAll();
    }

    private void PrintSelectedKeys()
    {
       printType = "SelectedNodesKey";
       printTexts = _tree.SelectedKeys;
    }

    private void SelectAll()
    {
        _tree.SelectAll();
    }

    private void DeselectAll()
    {
        _tree.DeselectAll();
    }

    private void ExpandAll()
    {
        _tree.ExpandAll();
    }

    private void CollapseAll()
    {
        _tree.CollapseAll();
    }

    private void ExpandAllChild()
    {
        _tree.ExpandAll(n => n.Key == "0-0");
    }

    private void CollapseAllChild()
    {
        _tree.CollapseAll(n => n.Key == "0-0");
    }

    private void OnCheck(TreeEventArgs<string> e)
    {
        Console.WriteLine("OnCheck:" + e.Node.Key);
    }

    private void OnSelect(TreeEventArgs<string> e)
    {
        Console.WriteLine("OnSelect:" + e.Node.Key);
    }

    private void OnExpand((string[] ExpandedKeys, TreeNode<string> Node, bool Expanded) e)
    {
        Console.WriteLine("OnExpand:" + JsonSerializer.Serialize(e.ExpandedKeys));
    }
}